var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../utils/util")), a = getApp(), e = require("../../../mybehaviors/location/location");

Page({
    behaviors: [ e ],
    data: {
        search: "",
        tabActive: 0,
        keyword: "",
        getBrand: [],
        brandNum: 11,
        goodsList: [],
        query: {
            page: 1,
            city: ""
        },
        totalPage: 0,
        isLoadMore: !0,
        brandId: "",
        categorys: [ {
            id: 0,
            name: "全部"
        } ],
        timer: "",
        categorysId: "",
        brandsId: ""
    },
    searchChange: function(a) {
        var e = this;
        this.setData({
            "query.page": 1,
            keyword: a.detail
        }), this.timer && clearTimeout(this.timer), this.timer = setTimeout(function() {
            t.default.debounce(e.goodsByList());
        }, 500);
    },
    exhibitionOn: function() {
        this.setData({
            brandNum: this.data.getBrand.length
        });
    },
    packup: function() {
        this.setData({
            brandNum: 11
        });
    },
    onLoad: function(t) {
        t.id && (this.setData({
            tabActive: Number(t.id),
            categorysId: Number(t.id)
        }), wx.setNavigationBarTitle({
            title: t.classifyName
        })), 0 === this.data.tabActive && this.setData({
            brandId: 0,
            brandsId: t.brandId || ""
        }), t.city && this.setData({
            "query.city": t.city
        }), this.categorysList();
    },
    brandClick: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            brandsId: a,
            "query.page": 1
        }), this.goodsByList();
    },
    categorysChange: function(t) {
        this.setData({
            "query.page": 1,
            categorysId: t.detail.name,
            tabActive: t.detail.index,
            brandId: t.detail.index
        }), this.goodsByList();
    },
    categorysList: function() {
        var t = this, e = a.getNetAddresss("plugin.yz-supply-cake.frontend.index.index.getCategorys");
        a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var o = e.data, s = t.data.categorys;
                    for (var i in o) s.push({
                        id: o[i].id,
                        name: o[i].name,
                        thumb: o[i].thumb,
                        thumb_url: o[i].thumb_url
                    });
                    t.setData({
                        categorys: s
                    });
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getBrandsData: function() {
        var t = this, e = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getBrand"), o = {
            city: this.data.query.city
        };
        a._getNetWork({
            url: e,
            data: o,
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    getBrand: e.data
                }) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    goodsByList: function() {
        var t = this, e = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsList");
        e += "&page=".concat(this.data.query.page), e += "&city=".concat(this.data.query.city), 
        e += "&search[brand_id]=".concat(this.data.brandsId), e += "&search[category]=".concat(this.data.categorysId), 
        e += "&search[keyword]=".concat(this.data.keyword), a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                wx.hideLoading(), wx.hideNavigationBarLoading(), wx.stopPullDownRefresh(), 1 == e.result ? (t.data.totalPage = e.data.last_page, 
                t.data.totalPage || (t.data.totalPage = 0), t.setData({
                    goodsList: e.data.data
                })) : (t.data.page = t.data.page - 1, t.isLoadMore = !1, wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getMoreData: function() {
        var t = this;
        if (this.data.isLoadMore = !1, !(this.data.query.page >= this.data.totalPage)) {
            this.data.query.page = this.data.query.page + 1;
            var e = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsList");
            e += "&page=".concat(this.data.query.page), e += "&city=".concat(this.data.query.city), 
            e += "&search[brand_id]=".concat(this.data.brandsId), e += "&search[category]=".concat(this.data.categorysId), 
            e += "&search[keyword]=".concat(this.data.keyword), console.log(this.data.query), 
            a._getNetWork({
                url: e,
                data: this.data.query,
                success: function(a) {
                    var e = a.data, o = e.data.data;
                    1 == e.result ? (t.data.isLoadMore = !0, t.setData({
                        goodsList: t.data.goodsList.concat(o)
                    })) : (t.data.query.page = t.data.query.page - 1, t.data.isLoadMore = !1, wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    }));
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onReady: function() {
        var t = this;
        try {
            this._getLocation(function(a, e) {
                var o = a.address_component.city;
                t.data.query.city || t.setData({
                    "query.city": o
                }), t.getBrandsData(), t.goodsByList();
            });
        } catch (t) {
            console.log(t), this.getBrandsData(), this.goodsByList();
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.setData({
            isLoadMore: !0,
            page: 1,
            totalPage: 0
        }), this.getMoreData();
    },
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多了");
    },
    onShareAppMessage: function() {}
});